<%@ page import="net.pws.oos.security.SecurityUtils"%>
<%@ page language="java" pageEncoding="UTF-8"%>
<%@ include file="../../header.jsp"%>

<div class="left ">
<div class="border bg">
<div class="page-header">
<h2 id="name">通知</h2>
<div class="cms_toolbar">
	<a href="javascript:;" onclick="goBack('./list.do')" class="btn"><i class="icon-list"></i>返回</a>
	<% if(SecurityUtils.allowedResource("/ot/notice/delete")){ %>
	<a id="deleteBtn" href="javascript:void(0);" class="btn btn-danger" onclick="doDelete()"><i class="icon-remove"></i>删除</a>
	<%} %>
</div>
</div>

<div id="noticeDetail">
	<p class="muted">发布人：<span id="creatorName"></span> 发布时间：<span id="createDate"></span></p>
</div>
<div id="noticeTabs">
	<ul class="nav nav-tabs">
		<li class="active"><a href="#contentDiv" data-toggle="tab">通知内容</a></li>
		<li><a href="#unreadDiv" data-toggle="tab">未读人员(<small id="unreadCount"></small>)</a></li>
		<li><a href="#readDiv" data-toggle="tab">已读人员(<small id="readCount"></small>)</a></li>
	</ul>
	
	<div class="tab-content">
	<div class="tab-pane active" id="contentDiv">
		<textarea id="content" style="width: 95%"></textarea>
	</div>
	
	<div class="tab-pane" id="unreadDiv">
		<table class="table table-striped cms-table" id="unreadTable">
			<thead>
				<tr>
					<th width="60">名称</th>
					<th>部门</th>
				</tr>
			</thead>
			<tbody>
			</tbody>
		</table>
	</div>

	<div class="tab-pane" id="readDiv">
		<table class="table table-striped cms-table" id="readTable">
			<thead>
				<tr>
					<th width="60">名称</th>
					<th>部门</th>
					<th width="130">阅读时间</th>
				</tr>
			</thead>
			<tbody>
			</tbody>
		</table>
	</div>
	</div>
	
</div>

</div>
</div>

<table id="unreadTemplate" class="hide">
  <tr>
    <td></td>
    <td></td>
  </tr>
</table>

<table id="readTemplate" class="hide">
  <tr>
    <td></td>
    <td></td>
    <td></td>
  </tr>
</table>

<script type="text/javascript">
function loadData() {
	block(".page-header");
	$.ajax({
		type: "GET",
		cache: false,
		url: "detail.json?id=" + id,
		dataType: "json",
		success: loadSucceedCallback,
		error:function(message){
			unblock(".page-header");
			$.alert(message);
		}  
	});	
}

function loadSucceedCallback(data) {
	unblock(".page-header");
	if (!data.succeed) {
		$.notice(data.message);
		return;
	}

	var notice = data.data;
	$("#name").text("通知 - " + notice.name);
	$("#creatorName").text(notice.creatorName);
	$("#createDate").text(notice.createDate);
	$("#content").val(notice.content);
	$("#content").focus();
	$("#content").attr("disabled", "disabled");
	$("#unreadCount").text(notice.unreadReceivers.length);
	$("#readCount").text(notice.readReceivers.length);
	
	$(notice.unreadReceivers).each(function(index, item) {
		buildUnread(item).appendTo($("#unreadTable").find("tbody"));
	});
	$(notice.readReceivers).each(function(index, item) {
		buildRead(item).appendTo($("#readTable").find("tbody"));
	});
	
	$("#noticeDetail").show();
	$("#noticeTabs").tab("show");
}

function buildUnread(item) {
	var taskEle = $("#unreadTemplate").clone();
	taskEle.find("tr").attr("id", "item" + item.id);
	var tdList = taskEle.find("td");
	$(tdList.get(0)).text(item.receiverName);
	$(tdList.get(1)).text(item.receiverOrganName);
	return taskEle.find("tr");
}

function buildRead(item) {
	var taskEle = $("#readTemplate").clone();
	taskEle.find("tr").attr("id", "item" + item.id);
	var tdList = taskEle.find("td");
	$(tdList.get(0)).text(item.receiverName);
	$(tdList.get(1)).text(item.receiverOrganName);
	$(tdList.get(2)).text(item.readDate);
	return taskEle.find("tr");
}

function doDelete() {
	if (!confirm('是否删除？\n请确认是否继续操作！')) {
		return;
	}
	
	block(".page-header");
	$.ajax({
		type: "POST",
		url: "delete.json",
		data: {"id": id},
		dataType: "json",
		success: function(data){
			unblock(".page-header");
			if(!data.succeed){
				$.alert("删除失败，原因为：\n" + data.message);
				return;
			}
			$.info("删除成功！");
			goBack("./list.do");
		},
		error: function(message){
			unblock(".page-header");
			$.alert("删除失败：" + message);
		}
	});
}

var id = request("id");
$(document).ready(function() {
	$("textarea").tah({
		moreSpace:30,
		maxHeight:65535
	});
	loadData();
});


</script>


<%@ include file="../../footer.jsp"%>